package j20251017;

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Object lock1=new Object();
        Object lock2=new Object();
        Thread t1=new Thread(()->{
            synchronized (lock1) {
                System.out.println("t1获取到lock1锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            synchronized (lock2) {
                System.out.println("t1获取到lock2锁");
            }
        });
        Thread t2=new Thread(()->{
            synchronized (lock1) {
                System.out.println("t2获取到lock2锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            synchronized (lock2) {
                System.out.println("t2获取到lock1锁");
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
